//
//  MMLaunchViewController.m
//  zhihuDaily
//
//  Created by 赖锦浩 on 16/6/11.
//  Copyright © 2016年 cc. All rights reserved.
//

#import "MMLaunchViewController.h"
#import "MMMainViewController.h"
#import <UIImageView+WebCache.h>
#import "MMLaunchTool.h"
#import "AppDelegate.h"
#import "MMHomeController.h"
#import "SYLeftDrawerController.h"
#import "MMNavigationController.h"
@interface MMLaunchViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@end

@implementation MMLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置背景图片
    [self setupBackgroundImageView];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    White_StatusBar;
}

/** 设置背景图片 */
- (void)setupBackgroundImageView
{
    //取出保存的url
    NSString *url = [[NSUserDefaults standardUserDefaults] stringForKey:@"launch"];
    
    //如果有缓存的图片,直接加载
    if (url) {
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:url]];
    }
    
    
    //发送网络请求
    [MMLaunchTool getLaunchImageWithCompleted:^(id obj) {
        [[NSUserDefaults standardUserDefaults]setObject:obj forKey:@"launch"];
        
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:obj]];
        
        
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [UIView animateWithDuration:2.0 animations:^{
                //动画设置背景图片的透明度和大小
                self.backgroundImageView.alpha = 0.5;
                self.backgroundImageView.transform = CGAffineTransformMakeScale(1.3, 1.3);
                
                
            } completion:^(BOOL finished) {
               //动画结束之后跳转
                [JHPushAnimation AnimationUpturnType:@"rippleEffect"];
                SYLeftDrawerController *drawerController = [[SYLeftDrawerController alloc]init];
                MMNavigationController *nav = [[MMNavigationController alloc]initWithRootViewController:[[MMHomeController alloc]init]];
//                MMMainViewController *vc = [[MMMainViewController alloc]initWithContentViewController:nav leftMenuViewController:drawerController rightMenuViewController:nil];
                MMMainViewController *vc = [[MMMainViewController alloc]initWithContentViewController:nav leftMenuViewController:drawerController ];
                drawerController.delegate  = vc;
                AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                
                delegate.mainViewController = vc;
                [UIApplication sharedApplication].keyWindow.rootViewController = vc;
                
            }];
            
            
            
        });
        
        
    } failure:^{
        MMLog(@"网络不给力~");
    }];
    

}


@end
